﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication4.ServiceReference1;

namespace SilverlightApplication4.Util
{
    public partial class SearchWindow : ChildWindow
    {
        public FrameworkElement element;
        public Object SelectedObject;
        public String strSearchTable;
        public IEnumerable datasource;
        public String Value = String.Empty;
        public SearchWindow(FrameworkElement _element, String _strSearchTable)
        {
            element = _element;
            strSearchTable = _strSearchTable;
            InitializeComponent();
            SearchTextBox.KeyDown += SearchTextBox_KeyDown;
            this.KeyDown += SearchWindow_KeyDown;
            SearchGrid.SelectionChanged += SearchGrid_SelectionChanged;
            SearchGrid.MouseLeftButtonDown += SearchGrid_MouseLeftButtonDown;
        }

        void SearchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Down || e.Key == Key.PageDown)
                SearchGrid.Focus();
            else if (e.Key == Key.Enter)
                Search();
        }

        protected override void OnClosed(EventArgs e)
        {
            if(SelectedObject == null|| DialogResult == false) Value = String.Empty;
            if (SelectedObject is PHIEU_NHAP)
                Value = ((PHIEU_NHAP)SelectedObject).ID;
            else if (SelectedObject is CA_NHAN)
                Value = ((CA_NHAN)SelectedObject).ID;
            else if (SelectedObject is CONG_TY)
                Value = ((CONG_TY)SelectedObject).ID;
            else if (SelectedObject is HANG_HOA)
                Value = ((HANG_HOA)SelectedObject).ID;

            base.OnClosed(e);
        }

        public void Search()
        {
            String searchPattern = SearchTextBox.Text;
            if (String.IsNullOrEmpty(searchPattern) == false)
                searchPattern = searchPattern.Trim();

            if (strSearchTable == "PHIEU_NHAP")
            {
                DALClient client = new DALClient();
                client.GetReceiptsAsync(searchPattern);
                client.GetReceiptsCompleted += client_GetReceiptsCompleted;
            }
            else if (strSearchTable == "HANG_HOA")
            {
                DALClient client = new DALClient();
                client.GetHang_HoasAsync(searchPattern);
                client.GetHang_HoasCompleted += client_GetHang_HoasCompleted;
            }
            else if (strSearchTable == "CA_NHAN")
            {
                DALClient client = new DALClient();
                client.GetCA_NHANsAsync(searchPattern);
                client.GetCA_NHANsCompleted += client_GetCA_NHANsCompleted;
            }
            else if (strSearchTable == "CONG_TY")
            {
                DALClient client = new DALClient();
                client.GetCONG_TYsAsync(searchPattern);
                client.GetCONG_TYsCompleted += client_GetCONG_TYsCompleted;
            }
        }

        void client_GetCONG_TYsCompleted(object sender, GetCONG_TYsCompletedEventArgs e)
        {
            datasource = e.Result;
            PopulateData("ID,Mã số|TenCongTy,Tên công ty|MST,Mã số thuế");
        }

        void client_GetCA_NHANsCompleted(object sender, GetCA_NHANsCompletedEventArgs e)
        {
            datasource = e.Result;
            PopulateData("ID,Mã số|HoTen,Họ và tên|NgaySinh,Ngày sinh");
        }

        void client_GetHang_HoasCompleted(object sender, GetHang_HoasCompletedEventArgs e)
        {
            datasource = e.Result;
            PopulateData("ID,Mã số|TenHangVN,Tên hàng hóa");
        }

        void client_GetReceiptsCompleted(object sender, GetReceiptsCompletedEventArgs e)
        {
            datasource = e.Result;
            PopulateData("ID,Mã số|NgayLapPhieu,Ngày lập phiếu");
        }

        void SearchGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (SearchGrid.SelectedItem != null)
                {
                    SelectedObject = SearchGrid.SelectedItem;
                    this.DialogResult = true;
                }
            }
        }

        void SearchWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                this.DialogResult = false;
        }

        void SearchGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
                SelectedObject = e.AddedItems[0];
        }

        public void PopulateData(String columns)
        {
            SearchGrid.Columns.Clear();
            SearchGrid.ItemsSource = datasource;
            SearchGrid.AutoGenerateColumns = false;

            String[] arrcolumn = columns.Split('|');
            foreach (String strCol in arrcolumn)
            {
                String[] arr = strCol.Split(',');

                DataGridTextColumn col = new DataGridTextColumn();
                col.IsReadOnly = true;
                col.Header = arr[1];
                col.Binding = new System.Windows.Data.Binding(arr[0]);
                SearchGrid.Columns.Add(col);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            Search();
        }
    }
}

